package com.ruoyi.web.core.config;

import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;

/**
 * @Description TODO
 * @Author liuhl
 * @Date 2024/6/11
 **/
@Component
@EnableScheduling
@Slf4j
public class TomcatPrintTask {

    //@Scheduled(cron = "0/30 * * * * ?")
    public void run(){
        ApplicationContext applicationContext = SpringUtil.getApplicationContext();
        if (applicationContext instanceof AnnotationConfigServletWebServerApplicationContext) {
            AnnotationConfigServletWebServerApplicationContext servletWebServerApplicationContext = (AnnotationConfigServletWebServerApplicationContext) applicationContext;
            TomcatWebServer webServer = (TomcatWebServer)servletWebServerApplicationContext.getWebServer();
            Executor executor = webServer.getTomcat().getConnector().getProtocolHandler().getExecutor();
            ThreadPoolExecutor poolExecutor = (ThreadPoolExecutor)executor;
            String tomcatLog = poolExecutor.toString();
            log.debug("tomcat-info: {}", tomcatLog);
        }
    }

}
